﻿using System;
using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.disco;
using agsXMPP.protocol.iq.roster;
using agsXMPP.protocol.x.muc;

namespace JabberBotCore
{
    public class JabberProtocol
    {
        private readonly XmppClientConnection _XmppCon;
        private bool _logged;


        public delegate void jProtDel(JProtEvArgs args);

        public event jProtDel OnMsg;

        public JabberProtocol()
        {
            var jid = new Jid("AliasWorldBot@gmail.com");

            _XmppCon = new XmppClientConnection
                           {
                               AutoAgents = false,
                               Server = jid.Server,
                               Username = jid.User,
                               Password = "best2rest",
                               AutoPresence = true,
                               AutoRoster = true,
                               AutoResolveConnectServer = true
                           };

            // read the jid from the console

            

            // Connect to the server now 
            // !!! this is asynchronous !!!
            try
            {
                _XmppCon.OnRosterStart += new ObjectHandler(xmppCon_OnRosterStart);
                _XmppCon.OnRosterItem += new XmppClientConnection.RosterHandler(xmppCon_OnRosterItem);
                _XmppCon.OnRosterEnd += new ObjectHandler(xmppCon_OnRosterEnd);
                _XmppCon.OnPresence += new PresenceHandler(xmppCon_OnPresence);
                _XmppCon.OnMessage += new MessageHandler(xmppCon_OnMessage);
                _XmppCon.OnLogin += new ObjectHandler(xmppCon_OnLogin);

                //_XmppCon.Open();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            
        }

        private void xmppCon_OnLogin(object sender)
        {
            _logged = true;
        }

        private void xmppCon_OnMessage(object sender, Message msg)
        {
            if(msg.Type != MessageType.chat)
                return;
            if (OnMsg != null) OnMsg(new JProtEvArgs(msg.From,msg.Body,this));
            return;
        }

        private void msgAnalizier(string from, string body)
        {
            if(body[0] != '!')
                return;
            var comands = body.Split(' ');
            var command = comands[0].Substring(1);
            switch (command)
            {
                case "TEST":
                    StartTest(from, body);
                    break;
            }
        }

        private void StartTest(string from, string body)
        {
            //var _jid = new Jid("birhoff@gmail.com");
            //pro.msgSend(_jid, "hello");
            body = "test succesfull";
            msgSend(new Jid(from), body);
        }

        private void xmppCon_OnPresence(object sender, Presence pres)
        {
            
        }

        private void xmppCon_OnRosterEnd(object sender)
        {
            
        }

        private void xmppCon_OnRosterItem(object sender, RosterItem item)
        {
            
        }

        private void xmppCon_OnRosterStart(object sender)
        {
            
        }

        public bool Connect()
        {
            _logged = false;
            _XmppCon.Open();
            while(!_logged)
            {
                System.Threading.Thread.Sleep(100);
            }
            
            //Connet to chat room 
            //TODO: need escape to new method
            var pres = new Presence();
            var to = new Jid("IDLEGame@conference.jabber.ru") {Resource = "IDLEGameMaster"};
            pres.To = to;
            _XmppCon.Send(pres);
            return true;
        }

        public bool msgSend(Jid _jid,string msg)
        {
            var _msg = new Message {Type = MessageType.chat, To = _jid, Body = msg};

            _XmppCon.Send(_msg);
            return true;
        }

       }
    public class JProtEvArgs : EventArgs
    {
        private readonly string _msg;
        private readonly String _owner;
        private readonly JabberProtocol _jprot;


        public JProtEvArgs(String owner, String msg, JabberProtocol _jprot)
        {
            _msg = msg;
            this._jprot = _jprot;
            _owner = owner;
        }

        public string Owner
        {
            get { return _owner; }
        }

        public string Msg
        {
            get { return _msg; }
        }

        public JabberProtocol JProt
        {
            get { return _jprot; }
        }
    }
}